// components/search/search.js

// 数据接口
const {get_searchKey} = require("../../utils/ajax")

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    searchKeys:[],
    searchKey:""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 输入联想提示
    myInput(e){
      this.setData({
        searchKey:e.detail.value
      },()=>{
        get_searchKey({key:this.data.searchKey}).then(res=>{
          console.log(res.data)
          this.setData({
            searchKeys:res.data.map(item=>item.split(/\n/)[0].split("=")[1])
          })
        })
      })
    },
    // 搜索歌曲
    search(e){
      //获取当前页路由地址
      var pages = getCurrentPages();
      var thisPage = pages[pages.length-1]
      var path = thisPage.route
      console.log(path)
      let key = e.currentTarget.dataset.key || this.data.searchKey
      if(path == "pages/searchMusic/searchMusic"){
        wx.redirectTo({
          url: '../../pages/searchMusic/searchMusic?key='+key
        })
      }else{
        wx.navigateTo({
          url: '../../pages/searchMusic/searchMusic?key='+key
        })
      }
      this.setData({
        searchKey: ""
      })
    }
  }
})
